package pack.persistence;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public final class Connexion {
	 private static boolean cree = false;
	 private static EntityManagerFactory fabrique=null;
	 private static EntityManager persistance=null;
	 
	 public Connexion(){
		 super();
		 if (cree==false){
			 fabrique = Persistence.createEntityManagerFactory("panelsdonneurs");
			 persistance = fabrique.createEntityManager();
			 cree=true;
		 }
	 }
	 
	 public static void persist(Object object){
		 persistance.getTransaction().begin();
		 persistance.merge(object);
		 persistance.getTransaction().commit();
	 }
	 
	 public static EntityManagerFactory getEmf(){
		 return fabrique;
	 }
	 
	 public EntityManager getEm(){
		 return persistance;
	 }
	 
	 public EntityManager beginT(){
		 persistance.getTransaction().begin();
		 return persistance;
	 }
	 
	 public static void endT(){
		 Connexion connexion = new Connexion();
		 connexion.getEm().getTransaction().commit();
	 }
}
